package bsts.view.util;

import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import bsts.view.util.GridColumn;
import bsts.view.util.GridControl;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import bsts.controller.CustomerController;
/*
 * Author 		: Lee Zhong De Rollei
 * Student ID	: A0079953L
 */
public class SearchCustomerFrm extends JFrame implements MouseListener{
	
	/*Local attributes=================================================================================================*/
	//required as it references serializable objects for binary saving
	private static final long serialVersionUID = 1L;
	
	private	GridControl gridFrm;
	private FieldPanel fpCustomer;

	/*Constructors=======================================================================================================*/
	public SearchCustomerFrm(FieldPanel fpCustomer) {
		//Get datasource to bind to common grid control
		this.fpCustomer =fpCustomer;		
		initialize();
	}
	
	private void initialize(){
		
		//Define the column to be binded to common grid control
		ArrayList<GridColumn> gridCol = new ArrayList<GridColumn>();
		gridCol.add(new GridColumn("Nric","getNric",50));
		gridCol.add(new GridColumn("SurName","getSurname",50));
		gridCol.add(new GridColumn("FirstName","getFirstname",90));
		gridCol.add(new GridColumn("Address","getAddress",160));
		gridCol.add(new GridColumn("Contact","getTelephoneNo",50));
		
		//create grind control
		gridFrm= new GridControl(600,500,new CustomerController(),"getCustomers",gridCol,true);		
		//assign listener to grid control
		gridFrm.getGrid().addMouseListener(this);
		
		//set the properites of search customer frame
		setSize(600,500);
		setTitle("Search Customers");
		setLayout(new BorderLayout());		
		this.getContentPane().add(gridFrm,BorderLayout.NORTH);		
		
	}
	
	public void mouseClicked(java.awt.event.MouseEvent e){	
		//Handle double click
		if (e.getClickCount() == 2) {
		 int row=gridFrm.getGrid().rowAtPoint(e.getPoint());
  		 //set selected value in caller textfield
		 String customer_id =gridFrm.getGrid().getValueAt(row,0).toString();
		 fpCustomer.setValue(customer_id);	
		 this.dispose();
		}
		 
	 }
	   

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}


}
